/*
* Copyright (c) 2007, Nubo Technologies
* 
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.nubotech.gwt.oss.client.s3;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.Response;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;
import com.nubotech.gwt.oss.client.Bucket;
import com.nubotech.gwt.oss.client.ObjectHolderResult;
import com.nubotech.gwt.oss.client.ObjectHolderResultCallback;
import com.nubotech.gwt.oss.client.ObjectHolder;
import com.nubotech.gwt.oss.client.OnlineStorageServiceError;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author jonnakkerud
 */
public class ObjectHolderListHandler extends AwsS3CallbackHandler {
    //2006-06-09T17:38:37.000Z
    public static final String LASTMODIFIED_PATTERN = "yyyy-MM-ddTHH:mm:ss";
    
    private ObjectHolderResultCallback cb;
    private String bucketName;
    protected List<ObjectHolder> objectHolderResultList = new ArrayList<ObjectHolder>();
    
    public ObjectHolderListHandler(ObjectHolderResultCallback cb) {
        this.cb = cb;
    }

    public void onResponseReceived(Request request, Response response) {
        ObjectHolderResult objectHolderResult;
        if (hasError(response)) {
            objectHolderResult = new ObjectHolderResult(objectHolderResultList, createError(response));
        }
        else {        
            Document doc = getXml(response);        
            if (doc != null) {
                createContent(doc);
            }
            objectHolderResult = new ObjectHolderResult(objectHolderResultList, null);
        }
        cb.onObjectHolderResult(objectHolderResult);
    }

    public void onError(Request request, Throwable ex) {
        cb.onObjectHolderResult(new ObjectHolderResult(objectHolderResultList, new OnlineStorageServiceError(ex)));
    }

    public void createContent(Document doc) {
        Element contentsElement = doc.getDocumentElement();
        // Must do this if you ever use a raw node list that you expect to be
        // all elements.
        XMLParser.removeWhitespace(contentsElement);
        
        // get the bucket name
        bucketName = getElementTextValue(contentsElement, "Name");

        NodeList list = contentsElement.getElementsByTagName("Contents");
        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            objectHolderResultList.add(createContent(n));
        }
        
        // handle CommonPrefixes
        list = contentsElement.getElementsByTagName("CommonPrefixes");
        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            objectHolderResultList.add(createPrefix(n));
        }
        
    }

    
    private ObjectHolder createContent(Node n) {
        ObjectHolder c = new ObjectHolder();
        c.setBucket(new Bucket(bucketName));
        String key = getElementTextValue((Element) n, "Key");
        c.setKey(key);
        String size = getElementTextValue((Element) n, "Size");
        c.setSize(Integer.parseInt(size));
        String lastModified = getElementTextValue((Element) n, "LastModified");
        Date d = parseDate(lastModified);
        c.setLastModified(d);
        String etag = getElementTextValue((Element) n, "ETag");
        c.setETag(etag);
        return c;
    }

    // like a directory
    private ObjectHolder createPrefix(Node n) {
        ObjectHolder c = new ObjectHolder();
        c.setBucket(new Bucket(bucketName));
        String key = getElementTextValue((Element) n, "Prefix");
        c.setKey(key);
        return c;
    }
    
    private Date parseDate(String lastModified) {
        DateTimeFormat formatter = DateTimeFormat.getFormat(LASTMODIFIED_PATTERN);
        
        String s = lastModified.substring(0, lastModified.lastIndexOf(":")+3);
        
        return formatter.parse(s);
    }
    
}
